package com.irdstudio.rsp.rule.api.rest;

import com.irdstudio.rsp.rule.common.BizUtil;
import com.irdstudio.rsp.rule.common.constant.Constant.RULE_COLL_STATE;
import com.irdstudio.rsp.rule.dao.domain.SRuleCollInfo;
import com.irdstudio.rsp.rule.service.facade.SRuleCollInfoService;
import com.irdstudio.rsp.rule.service.vo.SRuleCollInfoVO;
import com.irdstudio.sdk.beans.core.vo.ResponseData;
import com.irdstudio.sdk.beans.web.controller.AbstractController;
import com.irdstudio.sdk.ssm.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api")
public class SRuleCollInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("sRuleCollInfoServiceImpl")
	private SRuleCollInfoService sRuleCollInfoService;


	@Autowired
	protected HttpServletRequest httpRequest;



	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleCollInfoVO>> querySRuleCollInfoAll(
			SRuleCollInfoVO vo) {
		List<SRuleCollInfoVO> outputVo = sRuleCollInfoService.queryAllOwner(vo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键失效规则
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/info/invalid/{ruleCollId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<Integer> invalidRuleCollInfo(@PathVariable("ruleCollId") String ruleCollId) {
		SRuleCollInfoVO inVo = new SRuleCollInfoVO();
		inVo.setRuleCollId(new String(ruleCollId));
		inVo.setRuleCollState(RULE_COLL_STATE.INVALID.getCode());
        UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
        inVo.setUpdateUser(userInfo.getUserId());
        inVo.setUpdateTime(BizUtil.getCurrentTime());
		int result = sRuleCollInfoService.updateByPk(inVo);
		return getResponseData(result);
		
	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/info/{ruleCollId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SRuleCollInfoVO> queryByPk(@PathVariable("ruleCollId") String ruleCollId) {
		SRuleCollInfoVO inVo = new SRuleCollInfoVO();
		inVo.setRuleCollId(new String(ruleCollId));
		SRuleCollInfoVO outputVo = sRuleCollInfoService.queryByPk(inVo);
		return getResponseData(outputVo);

	}
	
	/**
	 * 根据主键删除信息
	 * @param sRuleCollInfo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRuleCollInfoVO inSRuleCollInfoVo) {
		try {
			int outputVo = sRuleCollInfoService.deleteByPk(inSRuleCollInfoVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			ResponseData<Integer> resp = getResponseData(-1);
			resp.setMessage(e.getMessage());
			return resp;
		}
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSRuleCollInfoVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRuleCollInfoVO inSRuleCollInfoVo) {

        UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
        inSRuleCollInfoVo.setUpdateUser(userInfo.getUserId());
        inSRuleCollInfoVo.setUpdateTime(BizUtil.getCurrentTime());
        try {
			int outputVo = sRuleCollInfoService.updateByPk(inSRuleCollInfoVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			ResponseData<Integer> resp = getResponseData(-1);
			resp.setMessage(e.getMessage());
			return resp;
		}

	}
	
	/**
	 * 新增数据
	 * @param inSRuleCollInfoVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRuleCollInfo(@RequestBody SRuleCollInfoVO inSRuleCollInfoVo) {

        UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
        inSRuleCollInfoVo.setCreateUser(userInfo.getUserId());
        inSRuleCollInfoVo.setCreateTime(BizUtil.getCurrentTime());
        inSRuleCollInfoVo.setUpdateUser(userInfo.getUserId());
        inSRuleCollInfoVo.setUpdateTime(BizUtil.getCurrentTime());
        try {
			int outputVo = sRuleCollInfoService.insertSRuleCollInfo(inSRuleCollInfoVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			ResponseData<Integer> resp = getResponseData(-1);
			resp.setMessage(e.getMessage());
			return resp;
		}

	}


	@RequestMapping(value="/s/rule/coll/copy", method=RequestMethod.POST)
	public @ResponseBody ResponseData<SRuleCollInfo> copySRuleCollInfo(@RequestBody SRuleCollInfoVO inSRuleCollInfoVo) {
		UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
		try {
			SRuleCollInfo copyInfo = sRuleCollInfoService.copyRuleCollInfo(inSRuleCollInfoVo, userInfo);
			return getResponseData(copyInfo);
		} catch (Exception e) {
			ResponseData<SRuleCollInfo> resp = getResponseData(null);
			resp.setMessage(e.getMessage());
			return resp;
		}
	}
}
